package configuration

import (
	"encoding/json"
	"fmt"
	"kop/modules/configuration/client"
	"kop/util/log"
)

func init() {

	var err error
	if err = json.Unmarshal(client.Load("BuildingTemplate"), &BuildingConfigs); err != nil {
		panic(err)
	}

	log.Info("Load BuildingTemplate.json success.")

	//********************************************************************//

	if err = json.Unmarshal(client.Load("BuildingAttribute"), &attributeConfigs); err != nil {
		panic(err)
	}

	log.Info("Load BuildingAttribute.json success.")

	//********************************************************************//

	if err = json.Unmarshal(client.Load("BuildingSailorEmploy"), &SailorEmployConfigs); err != nil {
		panic(err)
	}

	log.Info("Load BuildingSailorEmploy.json success.")

}

// 建筑配置
type (
	BuildingConf struct {
		ID                  int32
		Name                string
		Type                int32
		BuildingAttributeID int32 // 属性表的ID
	}
	// 建筑其他属性配置
	BuildingAttributeConf struct {
		ID           int32
		MoneyIncome  int64   // 银币收益
		MoneyRate    float64 // 银币收益比例
		MoneyRateAll float64 // 大厅等级对应银币收益比率
		Reputation   int32   // 升级声望
		MoneyCost    int64   // 升级建筑银币消耗数量
		ItemCost     int32   // 物品消耗数量
		SailorCost   int32   // 升级建筑需要水手数量
	}

	SailorEmployConfig struct {
		Number    int32 `json:"ID"`
		MoneyCost int64 // 升级建筑银币消耗数量
	}
)

var (
	BuildingConfigs     = map[int32]BuildingConf{}
	attributeConfigs    = map[int32]BuildingAttributeConf{}
	SailorEmployConfigs = map[int32]SailorEmployConfig{}
)

func GetByBuildingID(buildingID int32) BuildingConf {

	var conf, ok = BuildingConfigs[buildingID]
	if ok {
		return conf
	}

	panic(fmt.Errorf("BuildingID not found [%d]", buildingID))
}

func (c BuildingConf) GetAttrByLevel(level int32) BuildingAttributeConf {

	var conf, ok = attributeConfigs[c.BuildingAttributeID+level]
	if ok {
		return conf
	}

	panic(fmt.Sprintf("BuildingAttributeID not found [%d]", c.BuildingAttributeID+level))
}

func (c BuildingAttributeConf) Income() int64 {
	return c.MoneyIncome
}

// 招募水手需要的银币
func GetMoneyBySailorNumber(number int32) int64 {

	var conf, _ = SailorEmployConfigs[number]
	return conf.MoneyCost
}
